package com.blockchain.console.servlets.user;

import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.blockchain.common.enums.CertificateType;
import com.blockchain.common.enums.IsPass;
import com.blockchain.console.service.user.UserManage;
import com.blockchain.console.service.user.entity.User;
import com.blockchain.console.service.user.query.UserQuery;
import com.blockchain.framework.http.servlet.annotation.Right;
import com.blockchain.framework.service.ServiceSession;
import com.blockchain.util.parser.EnumParser;
import com.blockchain.util.parser.IntegerParser;

@Right(id = "C_USER_UPDATE", name = "用户列表-编辑")
public class UpdateUser extends AbstractUserServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void processGet(HttpServletRequest request, HttpServletResponse response, ServiceSession serviceSession)
			throws Throwable {
		UserManage userManage = serviceSession.getService(UserManage.class);
		int userid = IntegerParser.parse(request.getParameter("userid"));
		User user = userManage.get(userid);
		request.setAttribute("user", user);
		forwardView(request, response, getClass());
	}

	@Override
	protected void processPost(final HttpServletRequest request, final HttpServletResponse response,
			final ServiceSession serviceSession) throws Throwable {
		UserManage userManage = serviceSession.getService(UserManage.class);
		int userid = IntegerParser.parse(request.getParameter("userid"));
		userManage.updateUser(new UserQuery() {

			@Override
			public String getZjh() {
				return request.getParameter("zjh");
			}

			@Override
			public IsPass getUserState() {
				return null;
			}

			@Override
			public int getUserId() {
				return 0;
			}

			@Override
			public String getSjh() {
				return request.getParameter("sjh");
			}

			@Override
			public IsPass getSftgjrz() {
				return null;
			}

			@Override
			public String getName() {
				return request.getParameter("name");
			}

			@Override
			public Timestamp getCreateTimeStart() {
				return null;
			}

			@Override
			public Timestamp getCreateTimeEnd() {
				return null;
			}

			@Override
			public CertificateType getCertificateType() {
				return EnumParser.parse(CertificateType.class, request.getParameter("certificateType"));
			}

			@Override
			public IsPass getSfts() {
				return EnumParser.parse(IsPass.class, request.getParameter("tszh"));
			}

			@Override
			public int getZbxe() {
				return IntegerParser.parse(request.getParameter("zbxe"));
			}
		}, userid);
		sendRedirect(request, response, getController().getURI(request, UserList.class));
	}
}
